Imports System.ComponentModel
Imports Aricie.DNN.UI.Attributes
Imports Aricie.Collections
Imports Aricie.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports System.Xml.Serialization
Imports Aricie.DNN.UI.WebControls.EditControls
Imports Aricie.DNN.Caching.Client.Velocity
Imports Aricie.DNN.Caching.Client.Memcached

Namespace Aricie.DNN.Caching.Client
    <Serializable()> _
    Public Class CachingClientSettings
        Implements ITypedContainer


        Private _ClientProviders As New List(Of CachingClientProviderInfo)
        Private _Clusters As New SerializableList(Of CachingClusterSettings)
        Private _NewClusterProviderName As String = ""
        Private _AvailableProviders As SerializableDictionary(Of String, CachingClientProviderInfo)
        Private _AvailableClusters As SerializableDictionary(Of String, CachingClusterSettings)

        <ExtendedCategory("Clusters")> _
        Public Property Clusters() As SerializableList(Of CachingClusterSettings)
            Get
                Return _Clusters
            End Get
            Set(ByVal value As SerializableList(Of CachingClusterSettings))
                _Clusters = value
            End Set
        End Property

        <ExtendedCategory("Providers")> _
        <Editor(GetType(ListEditControl), GetType(EditControl))> _
        <TrialLimited(Security.Trial.TrialPropertyMode.NoAdd Or Security.Trial.TrialPropertyMode.NoDelete)> _
        Public Property ClientProviders() As List(Of CachingClientProviderInfo)
            Get
                Return _ClientProviders
            End Get
            Set(ByVal value As List(Of CachingClientProviderInfo))
                _ClientProviders = value
            End Set
        End Property


        <XmlIgnore()> _
        <ExtendedCategory("Clusters")> _
          <Editor(GetType(SelectorEditControl), GetType(EditControl))> _
            <Selector(GetType(CachingProviderSelectorControl), "Name", "Name", False, False, "", "", False, False)> _
        Public Property NewClusterProviderName() As String
            Get
                Return _NewClusterProviderName
            End Get
            Set(ByVal value As String)
                _NewClusterProviderName = value
            End Set
        End Property


        Private avProvidersLock As New Object


        <XmlIgnore()> _
            <Browsable(False)> _
        Public ReadOnly Property AvailableProviders() As SerializableDictionary(Of String, CachingClientProviderInfo)
            Get
                If _AvailableProviders Is Nothing Then
                    SyncLock avProvidersLock
                        If _AvailableProviders Is Nothing Then
                            Dim tempAv As New SerializableDictionary(Of String, CachingClientProviderInfo)
                            For Each providerConfig As CachingClientProviderInfo In Me.ClientProviders
                                If providerConfig.Enabled Then
                                    tempAv(providerConfig.Name) = providerConfig
                                End If
                            Next
                            _AvailableProviders = tempAv
                        End If
                    End SyncLock
                End If
                Return _AvailableProviders
            End Get
        End Property

        Private avClustersLock As New Object


        <XmlIgnore()> _
            <Browsable(False)> _
        Public ReadOnly Property AvailableClusters() As SerializableDictionary(Of String, CachingClusterSettings)
            Get
                If _AvailableClusters Is Nothing Then
                    SyncLock avClustersLock
                        If _AvailableClusters Is Nothing Then
                            Dim tempAv As New SerializableDictionary(Of String, CachingClusterSettings)
                            For Each objCluster As CachingClusterSettings In Me._Clusters
                                If objCluster.Enabled AndAlso objCluster.Hosts.Count > 0 Then
                                    tempAv(objCluster.ClusterName) = objCluster
                                End If
                            Next
                            _AvailableClusters = tempAv
                        End If
                    End SyncLock
                End If
                Return _AvailableClusters
            End Get
        End Property


        Public Function GetNewItem(ByVal collectionPropertyName As String) As Object Implements ITypedContainer.GetNewItem
            Select Case collectionPropertyName
                Case "Clusters"
                    Dim toReturn As CachingClusterSettings = Me.AvailableProviders(Me._NewClusterProviderName).GetClientProvider.GetNewCluster
                    toReturn.ProviderName = Me._NewClusterProviderName
                    Return toReturn
                Case Else
                    Return Nothing
            End Select
        End Function


        'Friend Sub AddDefaultProviders()
        '    Dim appProvider As New CachingClientProviderInfo
        '    appProvider.Name = "AppFabric 1.1"
        '    appProvider.Decription = "That client provider supports AppFabric distributed caching 1.1. Most features are supported."
        '    appProvider.TypeName = Aricie.Services.ReflectionHelper.GetSafeTypeName(GetType(AppFabricClientProvider)) ' "Aricie.DNN.Caching.Client.Velocity.VelocityClientProvider, Aricie.DNN.DistributedCachingProvider"
        '    appProvider.SupportsNotifications = True
        '    appProvider.SupportsRegions = True
        '    appProvider.SupportsSlidingExpiration = False
        '    appProvider.SupportsTimeOutResets = True
        '    appProvider.SupportsOptimisticConcurrency = True
        '    appProvider.SupportsPessimisticConcurrency = True
        '    appProvider.SupportsTags = True
        '    Me._ClientProviders.Add(appProvider)
        '    Dim vProvider As New CachingClientProviderInfo
        '    vProvider.Name = "Velocity"
        '    vProvider.Decription = "That client provider supports AppFabric code name ""Velocity"" CTP3 (later versions require .Net 4). All available features are supported."
        '    vProvider.TypeName = Aricie.Services.ReflectionHelper.GetSafeTypeName(GetType(VelocityClientProvider)) ' "Aricie.DNN.Caching.Client.Velocity.VelocityClientProvider, Aricie.DNN.DistributedCachingProvider"
        '    vProvider.SupportsNotifications = True
        '    vProvider.SupportsRegions = True
        '    vProvider.SupportsSlidingExpiration = False
        '    vProvider.SupportsTimeOutResets = True
        '    vProvider.SupportsOptimisticConcurrency = True
        '    vProvider.SupportsPessimisticConcurrency = True
        '    vProvider.SupportsTags = True
        '    Me._ClientProviders.Add(vProvider)
        '    Dim mProvider As New CachingClientProviderInfo
        '    mProvider.Name = "Memcached"
        '    mProvider.Decription = "That client provider supports Memcached. Multiple Gets require version 1.2.1 or above"
        '    mProvider.TypeName = Aricie.Services.ReflectionHelper.GetSafeTypeName(GetType(MemcachedClientProvider)) ' "Aricie.DNN.Caching.Client.Memcached.MemcachedClientProvider, Aricie.DNN.DistributedCachingProvider"
        '    mProvider.SupportsNotifications = False
        '    mProvider.SupportsRegions = False
        '    mProvider.SupportsSlidingExpiration = True
        '    mProvider.SupportsTimeOutResets = False
        '    mProvider.SupportsOptimisticConcurrency = True
        '    mProvider.SupportsPessimisticConcurrency = False
        '    mProvider.SupportsTags = False
        '    Me._ClientProviders.Add(mProvider)
        'End Sub


    End Class

End Namespace